package com.augurit.wf.bi.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.FileUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComPrintForm;
import com.augurit.wf.bi.convert.WfTemplateAttachmentRefConvertor;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfTemplateAttachmentRefService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfTemplateAttachmentRefForm;
import com.augurit.wf.common.dao.WfTemplateAttachmentRefDao;
import com.augurit.wf.common.entity.WfTemplateAttachmentRef;

@Service
@Transactional
public class WfTemplateAttachmentRefServiceImpl implements IWfTemplateAttachmentRefService {

	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private WfTemplateAttachmentRefDao wfTemplatePrintDao;
	
	public Page<WfTemplateAttachmentRefForm> search(
			Page<WfTemplateAttachmentRefForm> page, List<PropertyFilter> filters) {
		return null;
	}

	/**
	 * 
	 * 根据主键id获取业务流程打印参考form对象
	 * @param id 主键id
	 * 
	 */
	@Transactional(readOnly = true)
	public WfTemplateAttachmentRefForm get(Long id) {
		if(id != null){
			StringBuilder hql = new StringBuilder("select ref, temp.templateCode");
			hql.append(" from WfTemplateAttachmentRef ref, WfBusTemplate temp where ref.templateId = temp.id and ref.id = ?");
			Object[] objs =  (Object[])wfTemplatePrintDao.findUnique(hql.toString(), id);
			
			if(objs != null){
				WfTemplateAttachmentRef entity = (WfTemplateAttachmentRef)objs[0];
				String templateCode = (String)objs[1];
				WfTemplateAttachmentRefForm form = WfTemplateAttachmentRefConvertor.convertVoToForm(entity);
				form.setTemplateCode(templateCode);
				return form ;
			}
		}
		return null;
		
	}
	/**
	 * 分页查询业务流程打印参考记录
	 * @param page
	 * @prama  form
	 * @return 
	 */
	@Transactional(readOnly = true)
	public Page<WfTemplateAttachmentRefForm> search(Page<WfTemplateAttachmentRefForm> page, WfTemplateAttachmentRefForm form) {
		
		Page<WfTemplateAttachmentRef> pg = PageUtils.newPageInstance(page);
		
		String hql = " select ref, temp.templateCode from WfTemplateAttachmentRef ref, WfBusTemplate temp where ref.templateId = temp.id";
		
		List values = new ArrayList();
		if(form != null){
			if(form.getTemplateId() != null){
				hql += " and ref.templateId = ?";
				values.add(form.getTemplateId());
			}
		}
		
		//排序
		hql += HqlUtils.buildOrderBy(page, "ref");
		
		List objs = wfTemplatePrintDao.findPage(pg, hql, values).getResult();
		PageUtils.copy(pg, WfTemplateAttachmentRefConvertor.convertObjsToFormList(objs), page);
		return page;
	}
	
	/**
	 * 显示模板下的所有红头文件
	 * @param templateCode
	 * @return
	 */
	public List<WfTemplateAttachmentRefForm> listAllRedHeadFiles(String templateCode){
		if(templateCode != null && templateCode.trim().length() > 0){
			Long templateId = wfBusTemplateService.getByTemplateCode(templateCode).getId();
			String hql = " from WfTemplateAttachmentRef ref where ref.templateId = ? and ref.fileType = ?";
			List<WfTemplateAttachmentRef> entities = wfTemplatePrintDao.find(hql, templateId, WfTemplateAttachmentRefForm.RED_HEAD_FILE_TYPE);
			return WfTemplateAttachmentRefConvertor.convertToFormList(entities);
		}
		return null;
	}
	
	/**
	 * 获取打印模板
	 * @param templateCode
	 * @return
	 */
	public String getPrintTemplatePath(String templateCode){
		if(templateCode != null && templateCode.trim().length() > 0){
			Long templateId = wfBusTemplateService.getByTemplateCode(templateCode).getId();
			String hql = " from WfTemplateAttachmentRef ref where ref.templateId = ? and ref.fileType = ?";
			WfTemplateAttachmentRef entity = wfTemplatePrintDao.findUnique(hql, templateId, WfTemplateAttachmentRefForm.PRINT_TEMPLATE_TYPE);
			if(entity != null)
				return entity.getFilePath() + "/" + entity.getFileName();
		}
		return null;
	}
	
	/**
	 * 获取打印模板的相对路径
	 * @param templateId 业务模板Id
	 * @param busFormPrintCode 业务表单打印代码
	 */
	@Transactional(readOnly = true)
	public String getPrintTemplateRelativePath(Long templateId, String busFormPrintCode){
		if(templateId != null && StringUtils.isNotBlank(busFormPrintCode)){
			WfTemplateAttachmentRef entity = wfTemplatePrintDao.getBy(templateId, busFormPrintCode);
			return entity.getFilePath() + "/" + entity.getFileName();
		}
		return null;
	}
	
	public void deleteByTemplateId(Long templateId){
		if(templateId == null){
			throw new RuntimeException("template: "+  templateId + " is null");
		}
		
		List<WfTemplateAttachmentRef> entities = wfTemplatePrintDao.findBy("templateId", templateId);
		if(entities != null && entities.size() > 0)
			wfTemplatePrintDao.delete(entities);
	}

	public void delete(Long... ids) {
		wfTemplatePrintDao.delete(ids);
	}
	
	/**
	 * 级联删除
	 * @param id
	 */
	public void deleteCascade(Long id){
		if(id != null){
			WfTemplateAttachmentRefForm form = this.get(id);
			if(form != null){
				 String directPath = ServletActionContext.getServletContext().getRealPath("/");
				 String filePath = FileUtils.formatPath(directPath + form.getFilePath());
				 
				 File file = new File( filePath + "/" + form.getFileName());
				 
				 if(file.exists()){
					 file.delete();
				 }
				 this.delete(form.getId());
			}
		}
	}


	public void save(WfTemplateAttachmentRefForm... forms) {
		if(forms != null)
			for(WfTemplateAttachmentRefForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfTemplateAttachmentRefForm form){
		
		if(form != null){
			WfTemplateAttachmentRef entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = wfTemplatePrintDao.get(form.getId());
				
			}else{
				entity = new WfTemplateAttachmentRef();
			}
			//属性值转换
			WfTemplateAttachmentRefConvertor.convertFormToVo(form, entity);
			
			//保存
			wfTemplatePrintDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}
	
	
}
